package cn.xuchunh.campusbbs.ui.fragments;

import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cn.xuchunh.campusbbs.adapter.AlbumListAdapter;
import cn.xuchunh.campusbbs.base.BaseRecyclerViewAdapter;
import cn.xuchunh.campusbbs.base.BaseRefreshListFragment;
import cn.xuchunh.campusbbs.bean.Album;
import cn.xuchunh.campusbbs.bean.User;
import cn.xuchunh.campusbbs.net.HttpRequestManager;
import cn.xuchunh.campusbbs.ui.activities.PhotoListActivity;
import cn.xuchunh.campusbbs.utils.Constants;
import cn.xuchunh.campusbbs.utils.IntentParamsKey;
import cn.xuchunh.campusbbs.utils.JsonKey;
import cn.xuchunh.campusbbs.utils.JsonParser;
import cn.xuchunh.campusbbs.viewholder.AlbumViewHolder;

/**
 * Created by XuChunH on 2015/10/22.
 */
public class UserAlbumListFragment extends BaseRefreshListFragment<AlbumViewHolder, Album> {

    private User user;

    public static UserAlbumListFragment newInstance(User user) {

        Bundle args = new Bundle();
        args.putSerializable(IntentParamsKey.USER, user);
        UserAlbumListFragment fragment = new UserAlbumListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected RecyclerView.LayoutManager createLayoutManager() {
        return new GridLayoutManager(getActivity(), Constants.ALBUM_COLUMN_COUNT, LinearLayoutManager.VERTICAL, false);
    }

    @Override
    protected BaseRecyclerViewAdapter<AlbumViewHolder, Album> createAdapter() {
        return new AlbumListAdapter(getActivity());
    }

    @Override
    protected List<Album> parse(JSONObject jsonObject) throws JSONException {
        List<Album> list = new ArrayList<>();
        JSONArray jsonArray = jsonObject.optJSONArray(JsonKey.LIST);
        JSONObject albumJson;
        int length = jsonArray.length();
        for (int i = 0; i < length; i++) {
            albumJson = jsonArray.optJSONObject(i);
            Album album = new Album();
            album.setId(albumJson.optInt(JsonKey.ALBUM_ID));
            album.setUserId(albumJson.optInt(JsonKey.USER_ID));
            album.setReleaseDate(JsonParser.parseLong(albumJson, JsonKey.RELEASE_DATE));
            album.setLastUpdateDate(JsonParser.parseLong(albumJson, JsonKey.LAST_UPDATE_DATE));
            album.setUserName(albumJson.optString(JsonKey.USER_NICK_NAME));
            album.setTitle(albumJson.optString(JsonKey.TITLE));
            album.setInfo(albumJson.optString(JsonKey.INFO));
            album.setIcon(albumJson.optString(JsonKey.THUMB_PIC));
            list.add(album);
        }
        getAdapter().setHasMore(jsonObject.optInt(JsonKey.HAS_NEXT) != 0);
        return list;
    }


    @Override
    protected void loadDataFromServer() {
        HttpRequestManager.getInstance(getActivity()).loadUserAlbumList(
                user.getUserId(),
                getPage(),
                30,
                new ListXJsonListener(),
                new SimpleErrorListener(),
                getVolleyTag()
        );
    }

    @Override
    protected void loadDataFromCache() {

    }

    @Override
    public String getVolleyTag() {
        return UserAlbumListFragment.class.getSimpleName();
    }

    @Override
    public String getCacheKey() {
        return null;
    }

    @Override
    public void onItemClick(int position, View item) {
        Album album = getAdapter().getItem(position);
        PhotoListActivity.launch(getActivity(), user.getUserId(), album);
    }

    @Override
    protected void handleArgs(Bundle args) {
        if (args != null){
            user = (User) args.getSerializable(IntentParamsKey.USER);
            if(user == null){
                user = getUser();
            }
        }
    }

}
